<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <h2>表单输入绑定</h2>
      <!-- v-model的作用是做数据的双向绑定(变量改变，input的内容改变；input内容改变，变量改变) -->
      <input type="text" v-model="value" />
      <span>{{value}}</span>

      <hr />

      <!-- 自己实现v-model -->
      <input type="text" :value="value2" @input="handleInput" />
      <span>{{value2}}</span>

      <hr />

      <!-- 多行文本框使用v-model -->
      <textarea v-model="textValue"></textarea>
      <span>{{textValue}}</span>

      <hr />

      <!-- 单选框使用v-model -->
      <!-- <input type="radio" v-model="sex" value="1" />男 -->
      <input
        type="radio"
        name="sex"
        :checked="sex === '1'"
        @change="handleChange2"
        value="1"
      />男
      <input
        name="sex"
        type="radio"
        :checked="sex === '2'"
        @change="handleChange2"
        value="2"
      />女 {{sex}}
    </div>

    <script src="./Vue3.js"></script>
    <script>
      const { createApp } = Vue;

      createApp({
        data() {
          return {
            value: "123",
            value2: "abc",
            textValue: "",
            sex: "2",
          };
        },
        methods: {
          handleInput(e) {
            this.value2 = e.target.value;
          },
          handleChange2(e) {
            console.log(e.target.checked);
            this.sex = e.target.value;
          },
        },
      }).mount("#app");
    </script>
  </body>
</html>
